import mx.transitions.*;

function loadML2byPath(path:String)
{
	mlf.gatekeeper.native.Caller.MLoader(path);
}

function unloadML2bySlot(slot:String)
{
	mlf.mcmanager.SMCManager.popInstance(slot);
}

function accessSystemConfig(slot:String):Object
{
	if (slot!=null)
	{
		return mlf.mcmanager.MessageAgent.getMessageSlot("mlf", "config")[slot];
	}
	else return mlf.mcmanager.MessageAgent.getMessageSlot("mlf", "config");
}

function loadML2byContent(ml2Data:String)
{
	mlf.gatekeeper.native.Caller.MLoader(null, null, null, null, ml2Data);
}

function setValue(dataw:Object, slotName:String)
{
	for (var a in dataw)
	{
		if (dataw[a].type=="ml2ti")
		{
			var content=dataw[a].data;
			
			var eleArr=dataw[a].template.split("-");
			var container;
			
			container=scanElement(eleArr[0], eleArr[1]);
			// Packages.log("daR#@!TR@T!WE "+container, "debug");
			for (var a in container)
			{
				if (typeof(container[a])=="movieclip")
				{
					container[a].removeMovieClip();
				}
			}
			// Packages.log("EQTR#@!TR@T!WE "+container, "debug");
			// Packages.log("template: "+dataw[a].template, "debug");
			// Packages.log("ml2content: "+dataw[a].data, "debug");
			if (content!=null) loadML2byContent(content);
		}
		else
		{
			var ins=mlf.gatekeeper.native.Caller.scanElement(slotName, a);
			ins.text=dataw[a]?dataw[a]:"";
		}
	}
}

function scanElement(slot:String, ins:String)
{
	return mlf.gatekeeper.native.Caller.scanElement(slot, ins);
}

function dispatchMessage(obj:Object)
{
	mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch(obj);
}

function addListener(type:String, ins)
{
	mlf.mcmanager.MessageAgent.getMessageDispatcher().addEventListener(type, ins);
}

function removeListener(type:String, ins)
{
	mlf.mcmanager.MessageAgent.getMessageDispatcher().removeEventListener(type, ins);
}

function updatePortletView(slotName:String, oldPortletName:String, newPortletName:String)
{
	var transParams:Object={type:Iris, direction:Transition.OUT, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};

	var transListener:Object=new Object();
	transListener.slotName=slotName;
	transListener.oldPortletName=oldPortletName;
	transListener.newPortletName=newPortletName;
	transListener.allTransitionsOutDone=doUpdatePortletView;

	var oldMovieClip=Packages.system.mlf.GateKeeper.scanElement(slotName, oldPortletName);
	Packages.system.render.Transition.launch(oldMovieClip, transParams, transListener);
}

function doUpdatePortletView()
{
	var transParams:Object={type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};
	var oldMovieClip=Packages.system.mlf.GateKeeper.scanElement(this.slotName, this.oldPortletName);
	var newMovieClip=Packages.system.mlf.GateKeeper.scanElement(this.slotName, this.newPortletName);
	oldMovieClip._visible=false;
	newMovieClip._visible=true;
	Packages.system.render.Transition.launch(newMovieClip, transParams);
}

function updateDynamicPortletView(slotName:String, oldContent:String, newContent:String, data:String)
{
	// type - 0 : oldContent - view control with data
	// type - 1 : oldContent - lifecycle control no data
	
	var transParams:Object={type:Iris, direction:Transition.OUT, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};

	var transListener:Object=new Object();
	transListener.slotName=slotName;
	transListener.oldContent=oldContent;
	transListener.newContent=newContent;
	transListener.data=data;
	transListener.allTransitionsOutDone=doUpdateDynamicPortletView;
	
	var oldMovieClip=Packages.system.mlf.GateKeeper.scanElement(slotName, oldContent);
	Packages.system.render.Transition.launch(oldMovieClip, transParams, transListener);
}

function doUpdateDynamicPortletView()
{
	var oldMovieClip=Packages.system.mlf.GateKeeper.scanElement(this.slotName, this.oldContent);
	var newMovieClip=Packages.system.mlf.GateKeeper.scanElement(this.slotName, this.newContent);

	if (this.data==null)
	{
		oldMovieClip.removeMovieClip();
		newMovieClip._visible=true;
		var transParams:Object={type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};
		Packages.system.render.Transition.launch(newMovieClip, transParams);
	}
	else 
	{
		oldMovieClip._visible=false;
		loadML2byContent(this.data);
	}
}

function removePopup(oldSlot:String)
{
	// var transParams:Object={type:PixelDissolve, direction:Transition.OUT, duration:2, easing:Strong.easeOut, xSections:20, ySections:20};
	// var transParams:Object={type:Fade, direction:Transition.OUT, duration:2, easing:None.easeNone};
	var transParams:Object={type:Iris, direction:Transition.OUT, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};

	var transListener:Object=new Object();
	var oldMovieClip=scanElement(oldSlot, "basic");
	transListener.oldSlot=oldSlot;
	transListener.allTransitionsOutDone=doRemovePopup;
	
	Packages.system.render.Transition.launch(oldMovieClip, transParams, transListener);
}

function doRemovePopup()
{
	var oldSlot=this.oldSlot;
	unloadML2bySlot(oldSlot);
}

function updateDynamicPortlet(oldMovieClip:MovieClip, newURI:String, ml2Data:String, invokeCallObj:Object)
{
	var transParams:Object={type:Iris, direction:Transition.OUT, duration:2, easing:Strong.easeOut, startPoint:0, shape:Iris.CIRCLE};

	var transListener:Object=new Object();
	transListener.oldMovieClip=oldMovieClip;
	transListener.newURI=newURI;
	transListener.ml2Data=ml2Data;
	transListener.invokeCallObj=invokeCallObj;
	transListener.allTransitionsOutDone=doUpdateDynamicPortlet;
	
	Packages.system.render.Transition.launch(oldMovieClip, transParams, transListener);
}

function doUpdateDynamicPortlet()
{
	var oldMovieClip=this.oldMovieClip;
	var newURI=this.newURI;
	var ml2Data=this.ml2Data;
	var invokeCallObj=this.invokeCallObj;
	// mlf.mcmanager.component.IndexSlot.removeElement(mlf.mcmanager.PathResolver.getSMCName(oldMovieClip), oldMovieClip._name);

	oldMovieClip.unloadMovie();
	oldMovieClip.removeMovieClip();
	delete oldMovieClip;

	if (newURI!=null)
	{
		loadML2byPath(newURI);
	}
	else if (ml2Data!=null)
	{
		loadML2byContent(ml2Data);
	}

	if (invokeCallObj!=null)
	{
		invokeCallObj.target[invokeCallObj.func].apply(invokeCallObj.target, invokeCallObj.param);
	}
}

function invoke(target, func:String, args:Array)
{
	mlf.gatekeeper.native.Caller.callee(target, func, args);
}

function scanDictionary(path:MovieClip, key:String)
{
	return mlf.gatekeeper.native.Caller.scanDictionary(path, key);
}

function rtmpCaller(item:Object)
{
	var nc=Packages.services.RTMPConn.getInstance();
	if (nc.isConnected==true)
	{
		item.param.sessionId=Packages.model.Context.sessionID;
		if (item.type!=null) 
		{
			nc.call(item.type, item.result, item.param);
		}
	}
}

function playerController(type:String, param)
{
	return applications.jumpingnotes.alpha.Logic.run(type, param);
}